package j.quartz.frame.db.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import j.quartz.frame.db.enums.DataSourceType;


/**
 * 使用本地线程管理，dataSource上下文
 * 
 * @author songxudong[song_xd@suixingpay.com]
 */
public class DataSourceContextHolder {
	private static Logger LOGGER = LoggerFactory.getLogger(DataSourceContextHolder.class);

	// 线程本地环境
	private static final ThreadLocal<String> dsThreadLocal = new ThreadLocal<String>();

	public static ThreadLocal<String> getLocal() {
		return dsThreadLocal;
	}

	/**
	 * 读库
	 */
	public static void setRead() {
		dsThreadLocal.set(DataSourceType.READ.getType());
		LOGGER.info("======>>>>>>> Change database to Slave,will to read!");
	}

	/**
	 * 写库
	 */
	public static void setWrite() {
		dsThreadLocal.set(DataSourceType.WRITE.getType());
		LOGGER.info("======>>>>>>> Change Database to Master,will to write!");
	}	

	public static String getReadOrWrite() {
		return dsThreadLocal.get();
	}

	public static void clear() {
		dsThreadLocal.remove();
	}

}
